import {
  getPurchaseOrderDetailByPage,
  addPurchaseOrderDetail,
  updatePurchaseOrderDetail,
  getPurchaseOrderDetailByPoId,
} from '@/services/product/purchaseOrderDetail';
import { PageHeaderWrapper } from '@ant-design/pro-layout';
import type { ActionType, ProColumns } from '@ant-design/pro-table';
import ProTable from '@ant-design/pro-table';
import { Form, message } from 'antd';
import { sortTran, filterTran } from '@/utils/requestUtil';
import React, { useRef, useState, useImperativeHandle } from 'react';
const PurchaseOrderDetail = React.forwardRef((props, ref) => {
  const actionRef = useRef<ActionType>();
  const [modalVisit, setModalVisit] = useState(false); // 新建和编辑提示框
  const [confirmLoading, setConfirmLoading] = useState(false); //编辑框的disable
  const [formTitle, setFormTitle] = useState('新建'); //
  const columns: ProColumns[] = [
    {
      title: '序号',
      dataIndex: 'index',
      valueType: 'index',
      width: 80,
    },
    {
      title: '物料编号',
      dataIndex: 'materialCode',
      search: false,
    },
    {
      title: '物料名称',
      dataIndex: 'materialName',
      search: false,
    },
    {
      title: '销售订单号',
      align: 'center',
      sorter: true,
      dataIndex: 'so',
      search: false,
    },
    {
      title: '销售订单行',
      align: 'center',
      sorter: true,
      dataIndex: 'soitem',
      search: false,
    },
    {
      title: '规格型号',
      dataIndex: 'materialSpec',
      search: false,
    },
    {
      title: '品牌',
      dataIndex: 'brand',
      search: false,
    },
    {
      title: '数量',
      dataIndex: 'materialqty',
      search: false,
    },
    {
      title: '单位',
      dataIndex: 'unit',
      search: false,
    },
    {
      title: '未交货数量',
      dataIndex: 'undeliveredqty',
      search: false,
    },
    {
      title: '行业务关闭标志',
      dataIndex: 'isFinished',
      search: false,
    },
    {
      title: '预计到货时间',
      dataIndex: 'expectedDeliveryTime',
      search: false,
    },
    {
      title: '创建日期', //表头显示的名称
      search: false,
      dataIndex: 'createTime',
    },
    {
      title: '创建人', //表头显示的名称
      search: false,
      dataIndex: 'createName',
    },
    // {
    //   title: '操作',
    //  align: 'center',sorter: true,
    //   width: 100,
    //   valueType: 'option',
    //   key: 'option',
    //   fixed: 'right',
    //   render: (text, record, _, action) => [
    //     <a
    //       key="deleteKey"
    //       onClick={() => {
    //         Modal.confirm({
    //           title: '删除',
    //           content: '确定要删除吗',
    //           okText: '确认',
    //           cancelText: '取消',
    //           onOk: async () => {
    //             await delPurchaseOrderDetail({ id: record.id });
    //             actionRef.current.reload();
    //           },
    //         });
    //       }}
    //     >
    //       删除
    //     </a>,
    //   ],
    // },
  ];

  const handleSubmit = async (values) => {
    setConfirmLoading(true);
    if (formTitle == '新建') {
      const result = await addPurchaseOrderDetail(values);
      if (result != null) {
        message.success('添加成功');
      }
    } else {
      await updatePurchaseOrderDetail(values);
    }
    setConfirmLoading(false);
    setModalVisit(false);
    actionRef.current!.reload();
  };
  useImperativeHandle(ref, () => {
    return actionRef.current!.reload();
  });
  return (
    <PageHeaderWrapper title={false} breadcrumb={undefined}>
      <ProTable
        headerTitle="分料详情"
        actionRef={actionRef} //用于触发刷新操作等，看api
        rowKey="id" //表格行 key 的取值，可以是字符串或一个函数
        search={false}
        request={async (params = {}, sort, filter) => {
          const sorters = sortTran(sort);
          const filters = filterTran(params);
          let result;
          if (props.purchaseOrderId) {
            const purchaseOrderId = props?.purchaseOrderId;
            const type = 0;
            result = await getPurchaseOrderDetailByPoId(purchaseOrderId, type, {
              ...params,
              sorters: sorters,
              filters: filters,
            });
          } else {
            result = await getPurchaseOrderDetailByPage({
              ...params,
              sorters: sorters,
              filters: filters,
            });
          }
          return {
            data: result.data.content,
            total: result.data.totalElements,
            success: result.success,
          };
        }}
        columns={columns}
        scroll={{ x: 2000, y: 350 }}
        rowSelection={{}}
      />
    </PageHeaderWrapper>
  );
});
export default PurchaseOrderDetail;
